<template>
	<view class="search-box">
		<!-- 搜索框 -->
		<uni-search-bar @confirm="search" @input="input" :radius="100" cancelButton="none"></uni-search-bar>

		<!-- 搜索提示列表 -->
		<view class="sugg-list">
			<view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: "",
				searchResults: [],
			};
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				if (this.kw === "") {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: static;
		top: 0;
		z-index: 999;

	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #303133;
			display: flex;
			align-items: center;
			justify-content: space-between;

			/* 均匀排列每个元素 ,首个元素放置于起点，末尾元素放置于终点 */
			.goods-name {
				//文字不允许换行
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，用。。。代替
				text-overflow: cllipsis;
				margin-right: 3px;
			}
		}
	}
</style>
